#include "BMPRGBApp.h"
//#include"ui_QMainWindow.h"
#include"ui_BMPRGBApp.h"
#include<QPushButton>
#include<QFileDialog>
#include <QImage>
#include <QFile>
#include <QTextStream>
#include<QInputDialog>
BMPRGBApp::BMPRGBApp(QWidget* parent)
    : QMainWindow(parent)
   //, ui(new Ui::BMPRGBAppClass)
{
    ui.setupUi(this);
  
    
   
connect(ui.txtget, &QPushButton::clicked, this, &BMPRGBApp::convert);
    connect(ui.inverse, &QPushButton::clicked, this, &BMPRGBApp::inverseImage);
    connect(ui.sharpening, &QPushButton::clicked, this, &BMPRGBApp::sharpeningImage);
    connect(ui.edgedetection, &QPushButton::clicked, this, &BMPRGBApp::edgeDetectionImage);
    connect(ui.scaler, &QPushButton::clicked, this, &BMPRGBApp::scalerimage);
}



//初步功能：将BMP图片的RGB值转换成文本输出
void BMPRGBApp::convert() {
    QString bmpFilePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("BMP Files(*.bmp)"));
    if (bmpFilePath.isEmpty()) {
        return;
    }
    QString txtFilePath = QFileDialog::getSaveFileName(this, tr("保存文本文件"), "", tr("Text Files(*.txt)"));
    if (txtFilePath.isEmpty()) { return; }

    QImage image(bmpFilePath);
    if (image.isNull()) {
        qDebug() << "无法加载BMP图片：" << bmpFilePath;
        return;
    }
    QFile txtFile(txtFilePath);
    if (!txtFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "无法打开文本文件进行写入：" << txtFilePath;return;

    }
    QTextStream out(&txtFile);
    for (int y = 0;y < image.height();++y) {
        for (int x = 0;x < image.width();++x) {
            QRgb pix = image.pixel(x, y);
            int red = qRed(pix);
            int green = qGreen(pix);
            int blue = qBlue(pix);
            out << red << " " << green << " " << blue << "\n"
                ;
        }
    }
    txtFile.close();
    qDebug() << "转换成功！";
}
//对获取到的RGB值进行处理：锐化和反色
//锐化
void BMPRGBApp::sharpeningImage() {
    QString bmpFilePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("BMP Files(*.bmp)"));
    if (bmpFilePath.isEmpty()) {
        return;
    }
    QImage image(bmpFilePath);
    if (image.isNull()) {
        qDebug() << "无法加载 BMP 图片：" << bmpFilePath;
        return;
    }
    QImage sharpenedImage = sharpening(image);
    QString saveFilePath = QFileDialog::getSaveFileName(this, tr("保存图片"), "", tr("BMP Files(*.bmp)"));
    if (!saveFilePath.isEmpty()) {
        sharpenedImage.save(saveFilePath, "BMP");
        qDebug() << "锐化处理后的图片保存成功！";
    }
}
QImage BMPRGBApp::sharpening(const QImage& image)
{
    QImage sharpenedimage = image.copy();
    //锐化算子
    int kn[3][3] = {
        {0,-1,0},
        {-1,5,-1},{0,-1,0}
    };
    for (int y = 1;y < image.height() - 1;++y) {
        for (int x = 1;x < image.width() - 1;++x) {
            int redsum = 0, greensum = 0, bluesum = 0;
            for (int ky = 0;ky <= 2;++ky) {
                for (int kx = 0;kx <= 2;++kx)
                {
                    QRgb pix = image.pixel(x + kx - 1, y + ky - 1);
                    int red = qRed(pix);
                    int green = qGreen(pix);
                    int blue = qBlue(pix);
                    redsum += red * kn[ky][kx];
                    greensum += green * kn[ky][kx];
                    bluesum += blue * kn[ky][kx];
                }
            }
            redsum = qBound(0, redsum, 255);
            greensum = qBound(0, greensum, 255);
            bluesum = qBound(0, bluesum, 255);
            sharpenedimage.setPixel(x, y, qRgb(redsum, greensum, bluesum));
        }
    }
    return sharpenedimage;
}

// 反色
void BMPRGBApp::inverseImage() {
    QString bmpFilePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("BMP Files(*.bmp)"));
    if (bmpFilePath.isEmpty()) {
        return;
    }
    QImage image(bmpFilePath);
    if (image.isNull()) {
        qDebug() << "无法加载 BMP 图片：" << bmpFilePath;
        return;
    }
    QImage invertedImage = inverse(image);
    QString saveFilePath = QFileDialog::getSaveFileName(this, tr("保存图片"), "", tr("BMP Files(*.bmp)"));
    if (!saveFilePath.isEmpty()) {
        invertedImage.save(saveFilePath, "BMP");
        qDebug() << "反色处理后的图片保存成功！";
    }
}

QImage BMPRGBApp::inverse(const QImage& image) {
    QImage invertedimage = image.copy();
    for (int y = 0; y < image.height(); ++y) {
        for (int x = 0; x < image.width(); ++x) {
            QRgb pix = image.pixel(x, y);
            int red = 255 - qRed(pix);
            int green = 255 - qGreen(pix);
            int blue = 255 - qBlue(pix);
            invertedimage.setPixel(x, y, qRgb(red, green, blue));
        }
    }return invertedimage;
}
//边缘提取
void BMPRGBApp::edgeDetectionImage() {
    QString bmpFilePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("BMP Files(*.bmp)"));
    if (bmpFilePath.isEmpty()) {
        return;
    }
    QImage image(bmpFilePath);
    if (image.isNull()) {
        qDebug() << "无法加载BMP图片" << bmpFilePath;
        return;
    }
    QImage edgedetectedimage = edgedetection(image);
    QString saveFilePath = QFileDialog::getSaveFileName(this, tr("保存图片"), "", tr("BMP Files(*.bmp)"));
    if (!saveFilePath.isEmpty()) {
        edgedetectedimage.save(saveFilePath, "BMP");
        qDebug() << "边缘提取处理后的图片保存成功！";
    }
}
QImage BMPRGBApp::edgedetection(const QImage& initimage) {
    QImage edgedimage = initimage.copy();
    //引入Sobel算子
    int sx[3][3] = { -1,0,1,-2,0,2,-1,0,1 };//水平方向Gx
    int sy[3][3] = { -1,-2,-1,0,0,0,1,2,1 };//竖直方向Gy
    for (int y = 1; y < initimage.height() - 1; ++y) {
        for (int x = 1; x < initimage.width() - 1; ++x) {
            int rsx = 0, gsx = 0, bsx = 0;
            int rsy = 0, gsy = 0, bsy = 0;
            //卷积操作
            for (int ky = 0; ky <= 2; ++ky) {
                for (int kx = 0; kx <= 2; ++kx) {
                    QRgb pix = initimage.pixel(x + kx - 1, y + ky - 1);
                    int r = qRed(pix);
                    int g = qGreen(pix);
                    int b = qBlue(pix);
                    rsx += r * sx[ky][kx];
                    gsx += g * sx[ky][kx];
                    bsx += b * sx[ky][kx];
                    rsy += r * sy[ky][kx];
                    gsy += g * sy[ky][kx];
                    bsy += b * sy[ky][kx];
                }
            }
            int edgedred = qBound(0, static_cast<int>(qSqrt(rsx * rsx + rsy * rsy)), 255);
            int edgedgreen = qBound(0, static_cast<int>(qSqrt(gsx * gsx + gsy * gsy)), 255);
            int edgedblue = qBound(0, static_cast<int>(qSqrt(bsx * bsx + bsy * bsy)), 255);
            edgedimage.setPixel(x, y, qRgb(edgedred, edgedgreen, edgedblue));
        }
    }
    return edgedimage;
}
//scaler功能
void BMPRGBApp::scalerimage() {
    QString bmpFilePath = QFileDialog::getOpenFileName(this, tr("选择图片"), "", tr("BMP Files(*.bmp)"));
    if (bmpFilePath.isEmpty())return;
    QImage image(bmpFilePath);
    if (image.isNull()) {
        qDebug() << "无法加载BMP图片:" << bmpFilePath;
        return;
    }
    bool agree;
    double suofang = QInputDialog::getDouble(this, tr("缩放图片"), tr("请输入缩放比例："), 1.0, 0.01, 20.0, 2, &agree);
    if (agree) {
        if (suofang >= 0.01 && suofang <= 20.0) {
            int newwid = static_cast<int>(image.width() * suofang);
            int newhei = static_cast<int>(image.height() * suofang);
            QImage scaledImage = image.scaled(newwid, newhei, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            QString saveFilePath = QFileDialog::getSaveFileName(this, tr("保存图片"), "", tr("BMP Files(*.bmp)"));
            if (!saveFilePath.isEmpty()) {
                scaledImage.save(saveFilePath, "BMP");
                qDebug() << "缩放处理后的图片保存成功！";
            }// 输入的值在范围内
        }
        else {
            qDebug() << "抱一丝哈，办不到！";// 输入的值不在范围内
        }
    }
    else {
        return;// 点击了取消
    }
    
    
}

BMPRGBApp::~BMPRGBApp()
{}
